/**
 * Created by Been on 2017-10-14.
 */
import originJSONP from 'jsonp';
export default function jsonp (url, data, option) {
  url += (url.indexOf('?') < 0 ? '?' : '&') + concatUrl(data);
  return new Promise((resolve, reject) => {
    originJSONP(url, option, (err, data) => {
      // 如果成功，err == null
      if (!err) {
        resolve(data);
      } else {
        reject(err);
      }
    });
  });
}
// 拼接url
function concatUrl (data) {
  let url = '';
  for (let k in data) {
    if (data.hasOwnProperty(k)) {
      let value = data[k] === 'undefined' ? '' : data[k];
      url += `&${k}=${encodeURIComponent(value)}`;
    }
  }
  // substring()左闭右开
  return url ? url.substring(1) : '';
}
